package com.atguigu.dao.impl;

import com.atguigu.dao.SubtaskDao;
import com.atguigu.pojo.Subtask;

import java.util.List;


public class SubtaskDaoImpl extends BaseDao implements SubtaskDao {

    @Override
    public int addSubtask(Subtask subtask) {
        System.out.println("SubtaskDaoImpl程序在[" + Thread.currentThread().getName() + "]线程中");
        String sql = "insert into t_subtask(`id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result`) values(?,?,?,?,?,?,?,?);";
        return update(sql,subtask.getId(),subtask.getMainTaskId(),subtask.getMainTaskType(), subtask.getServerId(),subtask.getTargetNodeId(),subtask.getCommand(),subtask.getStatus(),subtask.getResult());
    }

    @Override
    public int deleteSubtaskById(String id) {
        String sql = "delete from t_subtask where id=?";
        return update(sql, id);
    }
    @Override
    public int deleteSubtaskByMainTaskId(String mainTaskId) {
        String sql = "delete from t_subtask where mainTaskId=?";
        return update(sql, mainTaskId);
    }
    @Override
    public int updateSubtask(Subtask subtask) {
        String sql = "update t_subtask set `mainTaskId`=?, `mainTaskType`=?,`serverId`=?,`targetNodeId`=?,`command`=?,`status`=?,`result`=? where id=?";
        return update(sql,subtask.getMainTaskId(),subtask.getMainTaskType(), subtask.getServerId(),subtask.getTargetNodeId(),subtask.getCommand(),subtask.getStatus(),subtask.getResult(), subtask.getId());
    }
    @Override
    public int setSubtaskStatusByMainTaskId(String mainTaskId,String status){
        String sql = "update t_subtask set `status`=? where `mainTaskId`=?";
        return update(sql,status,mainTaskId);
    }
    @Override
    public Subtask querySubtaskById(String id) {
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask where id=?";
        return queryForOne(Subtask.class, sql, id);
    }
    @Override
    public Integer querySubtasksByStatusNotTrue() {
        String sql = "select count(*) from t_subtask where status != 'true'";
        return ((Number)queryForSingleValue(sql)).intValue();
    }

    @Override
    public List<Subtask> querySubtasksByCommand(String subtaskCommand) {
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask where locate(?,command)>0";
        return queryForList(Subtask.class, sql, subtaskCommand);
    }

    @Override
    public List<Subtask> querySubtasksByServerId(String serverId) {// 查询全部
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask where serverId=?";
        return queryForList(Subtask.class, sql,serverId);
    }
    @Override
    public List<Subtask> queryNeedDoSubtasksByMainTaskId(String mainTaskId) {// 查询全部
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask where mainTaskId=?";
        return queryForList(Subtask.class, sql,mainTaskId);
    }
    @Override
    public List<Subtask> querySubtasksByMainTaskId(String mainTaskId) {// 查询全部
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask where mainTaskId=?";
        return queryForList(Subtask.class, sql,mainTaskId);
    }

    @Override
    public List<Subtask> queryNeedDoSubtasksByServerId(String serverId) {// 查询全部
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask where serverId=? and status != 'true'";
        return queryForList(Subtask.class, sql,serverId);
    }
    @Override
    public List<Subtask> querySubtasksByServerIdAndSubtaskCommand(String serverId,String subtaskCommand){
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask where serverId=? and locate(?,command)>0";
        return queryForList(Subtask.class, sql,serverId,subtaskCommand);
    }
    @Override
    public List<Subtask> getSpeciallyAppointedTargetNodeMainTaskResult(String targetNodeId,String mainTaskId){
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask where targetNodeId=? and mainTaskId=?";
        return queryForList(Subtask.class, sql,targetNodeId,mainTaskId);
    }
    @Override
    public List<Subtask> querySubtasks() {// 查询全部
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask";
        return queryForList(Subtask.class, sql);
    }

    @Override
    public Integer queryForPageTotalCount() {//查询总条目数,为page分片做准备
        String sql = "select count(*) from t_subtask";
        Number subtaskCount = (Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return subtaskCount.intValue();
    }

    @Override
    public List<Subtask> queryForPageItems(Integer begin, Integer pageSize) {
        String sql = "select `id`,`mainTaskId`,`mainTaskType`,`serverId`,`targetNodeId`,`command`,`status`,`result` from t_subtask limit ?,?";
        return queryForList(Subtask.class, sql, begin, pageSize);//查询数据片段
    }
}


